LUSION = GM

local emeta = FindMetaTable("Entity")

include("cl_scoreboard.lua")
--include("scoreboard/player_frame.lua")
--include("scoreboard/player_infocard.lua")
--include("scoreboard/player_row.lua")
--include("scoreboard/scoreboard.lua")
--include("scoreboard/vote_button.lua")
include("configuration/sh_configuration.lua")
include("sh_hooks.lua")
include("sh_meta.lua")
include("rpskin.lua")
include("shared.lua")
include("newvars.lua")

-- Todo (Besides everything): Jobs menu: Make all spawnicons the players model, only show jobs that the player has a flag for.


LUSION.Author = "Mongoose :v;  Ideas from: [NX] Insane; Matt Qc"
LUSION.Email = "na"
LUSION.Website = "facepunch"

local pmeta = FindMetaTable("Player")

pmeta.RealName = pmeta.Nick
pmeta.SteamName = pmeta.Nick	-- Meh, people like their darkrp.
function pmeta:Nick()
	local lusionn = self:GetNWString("Lusion_Nickname")
	if ( lusionn == nil or lusionn == "" ) then
		return self:RealName()
	else
		return lusionn
	end
end
pmeta.Name = pmeta.Nick
pmeta.GetName = pmeta.Nick
pmeta.RpName = pmeta.Nick

function pmeta:HasFlagCL( flag )
	if self:GetNWString("lusion_"..flag) == "1" then
		return true;
	end
	
	return false;
end

function LUSION:Initialize()
	surface.CreateFont( "Tahoma", 36, 1000, true, false, "DoorText" )
	surface.CreateFont( "Tahoma", 10, 1000, true, false, "MoneyText" )
	
	self.BaseClass:Initialize()
end

--function LUSION:SpawnMenuEnabled()
	-- Possibly later on if a prop whitelist is unwanted
	-- or we can set a variable to make it regular or just whitelist
	-- then we can make this return true ... or something
	
	
	--return false
--	return true -- Just until I get a functional spawning system and such working
--end
--function LUSION:SpawnMenuOpen()
--	return true
--end
function LUSION:OnContextMenuOpen()
	self.BaseClass:OnContextMenuOpen()
	
	local trace = {}
	trace.start = LocalPlayer():EyePos()
	trace.endpos = trace.start + LocalPlayer():GetAimVector() * 95
	trace.filter = LocalPlayer()
	local tr = util.TraceLine( trace )

	if not tr.Entity:IsValid() then return end
	if tr.Entity:IsRealDoor() then
		if tr.Entity.owner and tr.Entity.owner == LocalPlayer() then
			local DermaPanel = vgui.Create( "DFrame" )
			DermaPanel:SetPos( 50,50 )
			DermaPanel:SetSize( 200, 250 )
			DermaPanel:SetTitle( "Testing Derma Stuff" )
			DermaPanel:SetVisible( true )
			DermaPanel:SetDraggable( true )
			DermaPanel:ShowCloseButton( true )
			DermaPanel:MakePopup()
 
			local MenuButton = vgui.Create("DButton")
			MenuButton:SetParent( DermaPanel )
			MenuButton:SetText( "Menu >" )
			MenuButton:SetPos(25, 50)
			MenuButton:SetSize( 150, 175 )
			MenuButton.DoClick = function ( btn )
				local MenuButtonOptions = DermaMenu() -- Creates the menu
				MenuButtonOptions:AddOption("hello", function() Msg("Hello") end ) -- Add options to the menu
				MenuButtonOptions:AddOption("how", function() Msg("How") end )
				MenuButtonOptions:AddOption("are", function() Msg("Are") end )
				MenuButtonOptions:AddOption("you", function() Msg("You") end )
				MenuButtonOptions:AddOption("?", function() Msg("?") end )
				MenuButtonOptions:Open() -- Open the menu AFTER adding your options
			end
		end
	end
end

function emeta:IsRealDoor()
	if not ValidEntity(self) then return false end
	local class = self:GetClass()

	if class == "func_door" or class == "prop_door_rotating" or class == "func_door_rotating" or class == "prop_dynamic" then
		return true
	end
	return false
end


local function DrawDoorGUI()
	local trace = {}
	trace.start = LocalPlayer():EyePos()
	trace.endpos = trace.start + LocalPlayer():GetAimVector() * 95
	trace.filter = LocalPlayer()
	local tr = util.TraceLine( trace )
         
	if not tr.Entity:IsValid() then return end
    
	local doortext = "Testicles."
	if tr.Entity:IsRealDoor() then
		if tr.Entity.owner then
			doortext = "Owned by " .. tr.Entity.DoorOwner():Nick() or "Nobody."
		else
			doortext = "For Sale!"
		end
		draw.DrawText(doortext, "DoorText", ScrW() / 2, ScrH() / 2, Color(255, 123, 255, 255),TEXT_ALIGN_CENTER)
	end
end

-- Disable default hud after the resetting health is fixed.
local DefaultHUD = {"CHudHealth"} -- {} to make health show again.
hook.Add("HUDShouldDraw", "HideDefaultHUD", function(name)
	for k,v in pairs(DefaultHUD) do
		if name == v then return false end
	end
end)

function LUSION:HUDPaint()
	self.BaseClass:HUDPaint()
	
	DrawDoorGUI()
end

local function ClientsidePropMenu()
	local BPMF = vgui.Create("DFrame")
	BPMF:SetPos( 138,23 )
	BPMF:SetSize( 440, 500 )
	BPMF:SetTitle( "Buy Props - Man what u waiting for yo?" )
	BPMF:SetVisible( true )
	BPMF:SetDraggable( false )
	BPMF:ShowCloseButton( true )
		
	local BP_cooler = vgui.Create("DButton", BPMF)
	BP_cooler:SetPos(150, 33)
	BP_cooler:SetSize( 150, 80 )
	BP_cooler:SetText( "display cooler" )
	BP_cooler.DoClick = function()
		RunConsoleCommand("say", "lol")
	end	
end
concommand.Add("lau_script", ClientsidePropMenu)


local FOMDFrame
local function ClientsideHelpMenu()
	FOMDFrame = vgui.Create( "DFrame" )
	FOMDFrame:SetPos( (ScrW() / 2) - 575, (ScrH() / 2) - 350)
	FOMDFrame:SetSize( (ScrW() / 2) - 250, 300 )
	FOMDFrame:SetTitle( "F1 Menu - Help" )
	FOMDFrame:SetVisible( true )
	FOMDFrame:SetDraggable( false )
	FOMDFrame:ShowCloseButton( true )
	FOMDFrame:MakePopup()
	
	local MainW = (FOMDFrame:GetWide() / 2) +195
	local MainH = (FOMDFrame:GetTall() / 2) +126
			
	local PropertySheets = vgui.Create("DPropertySheet", FOMDFrame)
	PropertySheets:SetPos( 2, 22 )
	PropertySheets:SetSize( MainW, MainH )
	
	-- Help panel / Main panel
		local HelpPanel = vgui.Create("DPanel", PropertySheets)
		HelpPanel:SetPos( 25, 55 )
		HelpPanel:SetSize( (ScrW() / 2) - 20, (ScrH() / 2) - 110 )
		HelpPanel.Paint = function()
			surface.SetDrawColor( 25, 75, 50, 125 ) 
			surface.DrawRect( 0, 0, HelpPanel:GetWide(), HelpPanel:GetTall() )
		end
		
		local FOMDLabel1 = vgui.Create("DLabel", HelpPanel)
		FOMDLabel1:SetText( [[
			Watup!
			
			
			F1 = Help - This menu (yay!)
			F2 = Main - Money and Jobs
			F3 = Door - Buy/Sell a door
			
			F4 = Admin Menu - Administrators only
		
		
			
		
		
		
		
			(Or rp_close_f1 in console)
		]] )
	FOMDLabel1:SetPos(135, 33)
	FOMDLabel1:SizeToContents()

	local FOMDButtonHelp = vgui.Create("DButton", HelpPanel)
	FOMDButtonHelp:SetText( "Are you stuck?" )
	FOMDButtonHelp:SetPos(145, 193)
	FOMDButtonHelp:SetSize( (ScrW() / 10) - 19, (ScrH() / 10) - 54 )	-- 110, 20
	FOMDButtonHelp.DoClick = function()
		RunConsoleCommand("rp_close_f1")
	end
	
	PropertySheets:AddSheet("Info", HelpPanel, "gui/silkicons/user", false, false, "Information and help")
	--PropertySheets:AddSheet("Commands", hordiv, "gui/silkicons/page", false, false, "View a list of useful commands.")
	
	FOMDFrame:SetSkin("LusionRp_Skin")
end
usermessage.Hook("ShowHelp_Menu", ClientsideHelpMenu)
concommand.Add("rp_close_f1", function()
	FOMDFrame:SetVisible(false)
end)

local Main
local function MainMenu()
	
	Main = vgui.Create("DFrame")
	Main:SetPos( 50, 50 )
	Main:SetSize( (ScrW() / 2) - 50, (ScrH() / 2) + 87)
	Main:SetTitle("Public menu - Where nothing you want here, is here.")
	Main:SetDraggable( true )
	Main:MakePopup()
	-- +25
	local MainW = (Main:GetWide() / 2) +294
	local MainH = (Main:GetTall() / 2) +203
			
	local PropertySheets = vgui.Create("DPropertySheet", Main)
	PropertySheets:SetPos( 2, 22 )
	PropertySheets:SetSize( MainW, MainH )
			
	-- First Property (Main)
		local PanelFirst = vgui.Create("DPanel", PropertySheets)
		PanelFirst:SetPos( 58, 55 )
		PanelFirst:SetSize( (ScrW() / 2) - 110, (ScrH() / 2) - 110 )
		PanelFirst.Paint = function()
			surface.SetDrawColor( 25, 75, 50, 125 ) 
			surface.DrawRect( 0, 0, PanelFirst:GetWide(), PanelFirst:GetTall() )
		end
			
		--[[local MDivider = vgui.Create("DHorizontalDivider", PanelFirst)
		MDivider:SetPos( 351, 26 )
		MDivider:SetSize( 292, 269 )
		MDivider:SetLeftWidth(142)
		MDivider:SetLeft(PanelFirst)
		MDivider:SetRight(CreditsLabels)
		MDivider:SetDividerWidth(3)]]--
			
		local MSeperate1 = vgui.Create("DLabel", PanelFirst)
		MSeperate1:SetPos( 1, 32 )
		MSeperate1:SetColor(Color(15, 135, 45, 255))
		MSeperate1:SetText("_________________________________________________________________________________________________________")
		MSeperate1:SizeToContents()
		local MSeperate2 = vgui.Create("DLabel", PanelFirst)
		MSeperate2:SetPos( 351, 42 )
		MSeperate2:SetColor(Color(15, 135, 45, 255))
		MSeperate2:SetText([[
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|
				|]]
		)
		MSeperate2:SizeToContents() -- The above is terrible, but I can't figure out a better way.
			
 		local CreditsLabels = vgui.Create("DLabel", PanelFirst)
		CreditsLabels:SetPos( 359, 105 )
		CreditsLabels:SetText([[I'd like to give special thanks to the
		following people for making LusionRp happen:
			
			
			Insane (http://bit.ly/xxDcaC)
			  - Jobs, pushed me to finish, a good friend.			  
			Matt QC (http://bit.ly/y92oMm)
			  - Some roleplay ideas, a good friend.
			Comrade Rodrigo (http://bit.ly/wz2xxf) 
			  - RP/Menu ideas (not theme), beta tested.
			
			pwnalizor (STEAM_0:0:339980)
			  - Hella lot of bug testing, chill dude for realz!
			  - My new lover.
			
			PortalGod
			  - Gave hammer help, some RP ideas. <3
			FPtje
			  - Borrowed a snippet of code from him. :v
			Drakehawke
			  - Cool guy, helped out with some coding. ;)
		]])
		CreditsLabels:SizeToContents()
			
			
		local PlayerM = vgui.Create("DLabel", PanelFirst)
		PlayerM:SetPos( 200, 5 )
		PlayerM:SetText("My money is currently    " .. GAMEMODE.config["Currency"] .. LocalPlayer():GetNWInt("currency"))
		PlayerM:SizeToContents()
		local DepositB = vgui.Create("DButton", PanelFirst)
		DepositB:SetPos( 185, 24 )
		DepositB:SetSize( 192, (ScrH() / 10) - 56 )	-- 192, 18
		DepositB:SetText(" Deposit some money into a local ATM.")
		--DepositB:SizeToContents()
						
		local PlayerM2 = vgui.Create("DLabel", PanelFirst)
		PlayerM2:SetPos( 8, 52 )
		PlayerM2:SetText([[Configure the slider below for amount to give, then double
		click a player in the list below.]])
		PlayerM2:SizeToContents()
			
		local GiveM2 = vgui.Create("DNumSlider", PanelFirst)
		GiveM2:SetPos( 190, 67 )
		GiveM2:SetWide( 155 )
		GiveM2:SetText('Money amount config')
		GiveM2:SetMin( 1 )
		GiveM2:SetMax( 80000 )
		GiveM2:SetDecimals( 0 )
		GiveM2:SizeToContents()

		local GiveM = vgui.Create("DListView", PanelFirst)
		GiveM:SetPos( 8, 105 )
		GiveM:SetSize( 339, (ScrH() / 5) + 138 )	-- 339, 293
		GiveM:SetMultiSelect( false )
		GiveM:AddColumn("Rp Nick")
		GiveM:AddColumn("Steam name")
		GiveM:AddColumn("Status")
		for k,v in pairs(player.GetAll()) do
			if not #player.GetAll() == 1 then if v != LocalPlayer() then
				GiveM:AddLine(v:Nick(), v:RealName(), v:GetFriendStatus()) end
			else
				GiveM:AddLine(v:Nick(), v:RealName(), v:GetFriendStatus())
			end
		end
		GiveM.DoDoubleClick = function(parent, index, list)
			RunConsoleCommand("rp_givemoney", list:GetValue(2), GiveM2:GetValue())
		end
		--GiveM:SizeToContents()
			
	-- Second Property (Jobs)
			
		--[[ Start stolen code from darkrp.
			I cannot for the life of me figure out how to set individual positions
		]]--
		local hordiv = vgui.Create("DHorizontalDivider", PropertySheets)
		hordiv:SetLeftWidth(396)	 -- 370	
			
		function hordiv.m_DragBar:OnMousePressed() end
		
		hordiv.m_DragBar:SetCursor("none")
		local Panel
		local Information
			
		function hordiv:Update()
			if Panel and Panel:IsValid() then
				Panel:Remove()
			end
			Panel = vgui.Create( "DPanelList")
			Panel:SetSize(185, 540)
			Panel:SetSpacing(1)
			Panel:EnableHorizontal( true )
			Panel:EnableVerticalScrollbar( true )
			Panel:SetSkin("LusionRp_Skin")
		
		
			local Info = {}
			local model
			local modelpanel
			local function UpdateInfo(a)
				if Information and Information:IsValid() then
					Information:Remove()
				end
				Information = vgui.Create( "DPanelList" )
				Information:SetPos(404,0) 		-- 378
				Information:SetSize(185, 540)
				Information:SetSpacing(20)
				Information:EnableHorizontal( false )
				Information:EnableVerticalScrollbar( true )
				Information:SetSkin("LusionRp_Skin")
				
				function Information:Rebuild() -- YES IM OVERRIDING IT AND CHANGING ONLY ONE LINE BUT I HAVE A FUCKING GOOD REASON TO DO IT!
					local Offset = 0
					if ( self.Horizontal ) then
						local x, y = self.Padding, self.Padding;
						for k, panel in pairs( self.Items ) do
							local w = panel:GetWide()
							local h = panel:GetTall()
							if ( x + w  > self:GetWide() ) then
								x = self.Padding
								y = y + h + self.Spacing
							end
							panel:SetPos( x, y )
							x = x + w + self.Spacing
							Offset = y + h + self.Spacing
						end
					else
						for k, panel in pairs( self.Items ) do
							if not panel:IsValid() then return end
							panel:SetSize( self:GetCanvas():GetWide() - self.Padding * 2, panel:GetTall() )
							panel:SetPos( self.Padding, self.Padding + Offset )
							panel:InvalidateLayout( true )
							Offset = Offset + panel:GetTall() + self.Spacing
						end
						Offset = Offset + self.Padding	
					end
					self:GetCanvas():SetTall( Offset + (self.Padding) - self.Spacing ) 
				end
			
				if type(Info) == "table" and #Info > 0 then
					for k,v in ipairs(Info) do
						local label = vgui.Create("DLabel")
						label:SetText(v)
						label:SizeToContents()
						if label:IsValid() then
							Information:AddItem(label)
						end
					end
				end

				if model and type(model) == "string" and a ~= false then
					modelpanel = vgui.Create("DModelPanel")
					modelpanel:SetModel(model)
					modelpanel:SetSize(90,230)
					modelpanel:SetAnimated(true)
					modelpanel:SetFOV(90)
					modelpanel:SetAnimSpeed(1)
					if modelpanel:IsValid() then
						Information:AddItem(modelpanel)
					end
				end
				hordiv:SetLeft(Panel)
				hordiv:SetRight(Information)
			end
			UpdateInfo()		
		
			local function AddIcon(name, Salary, description, idname)
				local icon = vgui.Create("SpawnIcon")
				--icon:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
				icon:SetModel(LocalPlayer():GetModel())
			
				icon:SetIconSize(131) --122
				icon:SetToolTip()
				icon.OnCursorEntered = function()
					icon.PaintOverOld = icon.PaintOver 
					icon.PaintOver = icon.PaintOverHovered
			
				
					Info[1] = name
					Info[2] = Salary
					Info[3] = description
					
					model = LocalPlayer():GetModel()
				
					UpdateInfo()
				end
				icon.OnCursorExited = function()
					if ( icon.PaintOver == icon.PaintOverHovered ) then 
						icon.PaintOver = icon.PaintOverOld 
					end
					Info = {}
					if modelpanel and modelpanel:IsValid() and icon:IsValid() then
						modelpanel:Remove()
						UpdateInfo(false)
					end
				end
			
				icon.DoClick = function()
					RunConsoleCommand("rp_join"..idname)
					UpdateInfo(true)
				end
				
			
				if icon:IsValid() then
					Panel:AddItem(icon)
				end
			end
			-- end stolen code?
			for k,v in ipairs(Lusion_Teams) do
				if LocalPlayer():Team() != k then
					if v.needsFlag then
						if LocalPlayer():HasFlagCL( v.idName ) then
							AddIcon(" Name:  ".. v.menuName, " Salary: " .. v.salary, v.description, v.idName)
						end
					else
						AddIcon(" Name: ".. v.menuName, " Salary: " .. v.salary, v.description, v.idName)
					end
				end
			end
			PropertySheets:AddSheet("Main", PanelFirst, "gui/silkicons/user", false, false, "See money, Give Money, Deposit Money")
			PropertySheets:AddSheet("Jobs", hordiv, "gui/silkicons/page", false, false, "You may join any job that pops up in this menu.")
		
			Main:SetSkin("LusionRp_Skin")
		end
		hordiv:Update()
		return hordiv
end
usermessage.Hook("f2_mainmenu", MainMenu)
concommand.Add("rp_close_f2", function()
	Main:SetVisible( false )
end)
			
	-- Main tab:
		-- your money is:
		-- Give money to:
		-- Deposit money (when i do the bank that is)		
		-- Show link to website or some other crap
	-- Job tab:
		-- If the player has job flags (Flags will only be for jobs now) then show available jobs
	-- Request tab:
		-- Request a job flag, along with a note saying why that player should have a job flag
local function AdministrateMenu()
	local Main = vgui.Create("DFrame")
	Main:SetPos( 50, 50 )
	Main:SetSize( (ScrW() / 2) - 100, (ScrH() / 2) + 100)
	Main:SetTitle("Administration Menu - Lots of detail. Yus.")
	Main:SetDraggable( false )
	Main:MakePopup()
		
	local MainW = (Main:GetWide() / 2) +266
	local MainH = (Main:GetTall() / 2) +206
		-- Main tab:
			-- View all players money
			-- Add / Take players money
			
			-- Set max money a player can have
			-- Quick-kick
			
		-- If owner and not superadmin ( owner can only be set in a file )
			-- Flags tab:
				-- View all players flags
				-- Take / Give flags
			-- Administrate tab:
				-- Give/Take superadmin
				-- Give/Remove player spawning with certain weapons
				-- Player ban / unban
				-- Set temporary superadmin
			-- Blacklist tab:
				-- Blacklist certain names
					-- First names, Middle, and Last
			-- Model Whitelist tab:
				-- Add and remove prop models on the fly.
end
usermessage.Hook("f4_adminmenu", AdministrateMenu)
	

usermessage.Hook("PlayerNotify", function( um )
	notification.AddLegacy( um:ReadString(), NOTIFY_GENERIC, um:ReadShort() )
end)
--[[usermessage.Hook("PlayerChatColor", function( um )
	chat.AddText( Color(tonumber(um:ReadString()), tonumber(um:ReadString()), tonumber(um:ReadString())), um:ReadString() )
end)]]--
